#pragma once
#include<iostream>
#include<fstream>
#include<string>
class Util{
    public:
        static bool ReadFileContent(const std::string& filename,std::string* out){
            out->clear();
            //图片是二进制的不能以文本方式读取
            // std::ifstream file(filename);
            // if(!file.is_open()){
            //     return false;
            // }
            // std::string line;
            // while(std::getline(file,line)){
            //     *out+=line;
            // }
            // file.close();
            // return true;
            size_t size=std::stoi(FileSize(filename));
            if(size>0){
                std::ifstream file(filename,std::ios::binary);
                if(!file.is_open()){
                    return false;
                }
                out->resize(size);
                file.read((char*)out->c_str(),size);
                file.close();
            }
            else{
                return false;
            }

            return true;
        }
        static bool ReadOneLine(std::string *bigstr,std::string *out,const std::string &sep){//\r\n
            auto pos=bigstr->find(sep);
            if(pos==std::string::npos){
                //一行都没有
                return false;
            }
            *out=bigstr->substr(0,pos);
            bigstr->erase(0,pos+sep.size());
            return true;
        }
        static std::string FileSize(const std::string& filename){
            std::ifstream file(filename,std::ios::binary);
            if(!file.is_open()){
                return "-1";
            }
            file.seekg(0,std::ios::end);
            auto size=file.tellg();
            file.close();
            return std::to_string(size);
        }
        static std::string GetFileType(const std::string& filename){
            //./wwwroot/index.html
            auto pos=filename.rfind('.');
            if(pos==std::string::npos){
                return "text/html";
            }
            std::string suffix=filename.substr(pos+1);
            if(suffix=="html"||suffix=="htm"){
                return "text/html";
            }
            if(suffix=="css"){
                return "text/css";
            }
            if(suffix=="js"){
                return "text/javascript";
            }
            if(suffix=="png"){
                return "image/png";
            }
            if(suffix=="jpg" || suffix=="jpeg"){
                return "image/jpeg";
            }
            if(suffix=="gif"){
                return "image/gif";
            }
            if(suffix=="ico"){
                return "image/x-icon";
            }
            if(suffix=="svg"){
                return "image/svg+xml";
            }
            if(suffix=="ttf"){
                return "application/x-font-ttf";
            }
            if(suffix=="woff"){
                return "application/font-woff";
            }
            if(suffix=="woff2"){
                return "application/font-woff2";
            }
            if(suffix=="eot"){
                return "application/vnd.ms-fontobject";
            }
            if(suffix=="otf"){
                return "application/x-font-otf";
            }
            if(suffix=="json"){
                return "application/json";
            }
            if(suffix=="xml"){
                return "application/xml";
            }
            if(suffix=="pdf"){
                return "application/pdf";
            }
            if(suffix=="zip" || suffix=="gz" || suffix=="tar" || suffix=="rar"){
                return "application/octet-stream";
            }
            return "text/plain";
        }
};